import {
	mapMutations
} from 'vuex'
import {
	Stomp
} from '@/websocket/stomp.js'
import WebSocket from '@/websocket/websocket.js'
export default {
	...mapMutations([
		'upUserInfo', 'upLastInformation'
	]),
	toSendInfo(e) {
		this.hanlderShop([e]);
		this.clearLastInformation();
	},
	clearLastInformation() {
		this.upLastInformation(null)
	},
	connect() {
		WebSocket.init().then((client) => {
			console.log("初始化获取", client)
			client.subscribe( // 订阅
				`/topic/message/${this.userInfo.id}/${this.u_id}`,
				(frame) => {

					let params = JSON.parse(frame.body)
					this.messageList.push(params)
					console.log(params) // 接收到的数据
					this.changeScrollTop()
					this.readMessage(params.sendUserId)
				}, {
					ack: 'client'
				} // 消息不会被确认接收，不确认每次连接都会推送
			)
		})
	},
	connectlisenter() {
		// 监听 WebSocket 连接关闭事件
		WebSocket.setCloseCallback((res) => {
			console.log('WebSocket 已关闭00！', res);
			// 在此处添加关闭事件的逻辑处理
			// 重连
			this.reconnectId = setTimeout(() => {
				this.reconnect()
			}, this.reconnectInterval)
		});
	},
	//重新连接
	reconnect() {

		this.connect()
	},
	unsubscribe() {
		WebSocket.disconnect()
		// 停止重连事件
		if (this.reconnectId) {
			clearTimeout(this.reconnectId)
			this.reconnectId = null
		}
	},
	readMessage(id) {
		let that = this
		that.$httpRequest.get({
			url: 'wechat/readMessage',
			that,
			params: {
				id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {

				}
			}
		})
	},

	//
	sendMoney(e) {
		let that = this
		that.$httpRequest.request({
			url: 'wechat/createPacketOrder',
			that,
			params: {
				total: e,
				toUser: that.u_id,
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.wechatPay(res.data)
				}
			}
		})
	},
	wechatPay(orderId) {
		let that = this

		if (that.loading) {
			that.$toast.msg('请求中...', {
				icon: 1
			})
			return false
		}
		that.loading = true
		that.$httpRequest.request({
			that: that,
			url: 'wechat/orderPay',
			params: {
				id: orderId ? orderId : that.orderId
			},
			success: res => {
				that.loading = false
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					// that.upSuccessPay(true)
					let data = res.data
					uni.requestPayment({
						timeStamp: data.timeStamp,
						nonceStr: data.nonceStr,
						package: data.package,
						signType: data.signType,
						paySign: data.paySign,
						success: res => {
							const info = {}
							info.type = 4
							info.toUserId = that.u_id
							info.sendUserAvatar = that.myUserinfo.avatar
							info.id = that.messageList.length > 0 ? that.messageList[that
									.messageList.length - 1]
								.id + 1 : 0;
							info.createTime = this.$utils.formatDateTime()
							that.messageList.push(info)
							that.$nextTick(() => {
								that.changeScrollTop()
							})
							// that.$toast.alert({
							// 	content: '支付成功',
							// 	callback: result => {
							// 		if (result.confirm) {
							// 			// that.Unlock = false
							// 			// that.$refs.popup.close()
							// 		}
							// 	}
							// })
						}
					})
				} else {
					that.$toast.alert({
						content: res.msg,
						callback: result => {

						}
					})
				}
			}
		})

	},
	//接受红包
	hanlderMoney(e) {
		let that = this
		if (e.isReceive) {
			return false
		}
		that.$httpRequest.request({
			url: 'wechat/receivePacket',
			that,
			params: {
				id: e.packetId,
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.messageList.forEach((item) => {
						if (item.packetId == e.packetId) {
							item.isReceive = true
						}
					})
				}
			}
		})
	},
	submitForm() {

		let that = this
		if (that.chartMessgae !== '') {
			if (that.atLoading) {
				//禁止多次点击
				return false
			}
			that.atLoading = true
			let params = {
				toUserId: that.u_id,
				type: 1,
				content: that.chartMessgae,
				sendUserAvatar: that.myUserinfo.avatar
			}
			that.chartMessgae = ''
			params.id = that.messageList.length > 0 ? that.messageList[that.messageList.length - 1].id + 1 : 0;
			params.createTime = this.$utils.formatDateTime()
			that.messageList.push(params)
			that.$nextTick(() => {
				that.changeScrollTop()
			})
			that.$httpRequest.request({
				that,
				url: 'wechat/sendMessage',
				params: params,
				success: res => {
					if (res.code === that.$httpConfig.SUCCESS_CODE) {
						// that.chartMessgae = ''
						// that.messageList.push(content)
						// that.changeScrollTop()
					}
				}
			})
			setTimeout(() => { //延时指定时间后，再解冻
				that.atLoading = false;
			}, 1000);
		}
	},
	handlerUpload(e) {
		let that = this
		let params = {
			toUserId: that.u_id,
			type: 2, //图片
			img: e.filePath,
			sendUserAvatar: that.myUserinfo.avatar
		}
		that.$httpRequest.request({
			that,
			url: 'wechat/sendMessage',
			params: params,
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					params.id = that.messageList.length > 0 ? that.messageList[that.messageList.length - 1]
						.id + 1 : 0;
					params.createTime = this.$utils.formatDateTime()
					that.messageList.push(params)
					that.$nextTick(() => {
						that.changeScrollTop()
					})
				}
			}
		})
	},
	hanlderShop(e) {
		const info = e[0];
		let that = this
		let params = {
			toUserId: that.u_id,
			type: 3, //笔记
			informationId: info.id,
		}
		that.$httpRequest.request({
			that,
			url: 'wechat/sendMessage',
			params: params,
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					info.type = 3
					info.toUserId = that.u_id
					info.sendUserAvatar = that.myUserinfo.avatar
					info.informationId = info.id
					info.createTime = this.$utils.formatDateTime()
					info.id = that.messageList.length > 0 ? that.messageList[that.messageList.length - 1]
						.id + 1 : 0
					that.messageList.push(info)
					that.$nextTick(() => {
						that.changeScrollTop()
					})
				}
			}
		})
	},
	hideOtherSend() {
		this.$refs.chatInputRef.setShowOther(false)
	},
	getPageData() {
		let that = this
		that.$httpRequest.get({
			url: 'wechat/getUserAllMessage',
			params: {
				userId: that.u_id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					uni.hideLoading()
					that.messageList = res.data
					that.$nextTick(() => {
						that.changeScrollTop()
					})
					that.lastMark = that.messageList.length > 0 ? that.messageList[that.messageList.length -
						1].id : 0
					// console.log(that.messageList,"that.messageList")
				} else {
					that.messageList = []
				}
			}
		}, false)
		// setTimeout(function () {
		//     that.$toast.msg('当前网络过慢...', {
		//     	icon: 1
		//     })
		// }, 5000);

	},
	changeTime() {
		this.timeRequest = setInterval(() => {
			this.getPageData()
			// console.log("启动定时器")
		}, 1700)
	},
	getChatUserInfo() {
		let that = this
		that.$httpRequest.get({
			url: 'wechat/getChatUserInfo',
			that,
			params: {
				id: that.u_id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.chatUser = res.data
				}
			}
		})
	},
	getUserInfo() {
		let that = this
		that.$httpRequest.get({
			that: that,
			url: 'wechat/info',
			params: {

			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.upUserInfo(res.data)
					that.myUserinfo = res.data
					// setTimeout(function () {
					// 		    uni.hideLoading();
					// 		}, 200);
				} else {
					that.upUserInfo({})

				}
			}
		})
	},
	changeScrollTop() {
		this.scrollTop = this.messageList.length * 1700
	},
	//关注
	follow(id) {
		let that = this
		that.$httpRequest.request({
			that: that,
			url: 'wechat/follow',
			params: {
				followUserId: id
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.chatUser.follow = true

				}
			}
		})
	},
	unfollow(id) {
		let that = this
		that.$toast.confirm({
			content: '确定取消关注？',
			confirmText: '确定',
			callback: result => {
				if (result) {
					that.$httpRequest.request({
						that: that,
						url: 'wechat/unfollow',
						params: {
							followUserId: id
						},
						success: res => {
							if (res.code === that.$httpConfig.SUCCESS_CODE) {
								that.chatUser.follow = false

							}
						}
					})
				} else {

				}
			}
		})
	},
}